<?php
/**
 * Validate class.
 *
 * @package SGN_SMS
 * @version $Id: Validate.php 1597 2012-10-12 04:09:18Z tamlt2 $
 * @author tamlt2 $
 *
 */
class SGN_SMS_Validate
{    
    /**
     *
     * @var $_instance
     */
    private static $_instance = null;
    
    /**
     * Singleton pattern.
     *
     * @return Validate
     */
    public static function getInstance()
    {
        if (self::$_instance === null)
            self::$_instance = new self();
        return self::$_instance;
    }
    
    /**
     * Constructor 
     */
    public function __construct()
    {

    }

    /**
     * Check Number is from Viet Nam
     */
    public function checkNumber($number, $convert = true)
    {
        /**
         * Get Config
         */
        $config = SGN_Application::getConfig('sgn', 'sms', 'mobilephone');
        /**
         * Remove character not number
         */
        $number = preg_replace("/[^0-9]/", "", $number);
        /**
         * Check Length
         */
        if(strlen($number) < $config->phonenumber->minlength && strlen($number) > $config->phonenumber->maxlength) {
            return false;
        }
        /**
         * Check Phone Prefix
         */
        if(!$this->checkPrefix($number)) {
            return false;
        }
         
        /**
         * Convert Phone Number
         */
        if($convert) {
            return $this->convertNumber($number, false);
        }
        return true;
    }
    
    /**
     * Check phone Prefix support SMS
     * @param $number mobile phone number
     */
    public function checkPrefix($number)
    {
        /**
         * Remove Country Code
         */
        if(substr($number, 0, 2) == 84) {
            $number = substr($number, 2, strlen($number));
        } else if(substr($number, 0, 1) == 0) {
            $number = substr($number, 1, strlen($number));
        }
        /**
         * Check Number Prefix
         */
        if(strlen($number) == 9) {
            $numPrefix = substr($number, 0, 2);
        } else if(strlen($number) == 10) {
            $numPrefix = substr($number, 0, 3);
        } else {
            /**
             * False
             */
            return false;
        }
        /**
         * Get Config
         */
        $config = SGN_Application::getConfig('sgn', 'sms', 'mobilephone');
        /**
         * All Number Prefix
         */
        $listPrefix = array();
        $phonePrefix = $config->numberPrefix->toArray();
        if(is_array($phonePrefix) && count($phonePrefix) > 0) {
            foreach($phonePrefix as $prefix) {
                $prefix = explode(',', str_replace(' ', '', $prefix));
                $listPrefix = array_merge($listPrefix, $prefix);
            }
            $listPrefix = array_unique($listPrefix);
        }
        /**
         * Check Prefix Exist
         */
        if(in_array($numPrefix, $listPrefix)) {
            return true;
        }
        return false;
    }
    
    /**
     * Convert Number to Viet Nam Phone
     */
    public function convertNumber($number, $checkNumber = true)
    {
        if($checkNumber && !$this->checkNumber($number, false)) {
            return false;
        }
        /**
         * If exist country code
         */
        if(substr($number, 0, 2) == 84) {
            return $number;
        } 
        /**
         * If don't exist country code
         */
        else if(substr($number, 0, 1) == 0) {
            $number = substr($number, 1, strlen($number));
            return '84'.$number; 
        }
    }
}